package tdca.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

import java.io.IOException;
import tdca.util.DataUtil;

public class Capture2DCode extends Activity {
    private Button btn_capture_master_code;
    private Button btn_capture_private_code;
    private Button btn_capture_slave_code;
    private Button btn_capture_return;

    private TextView textView_capture_result_master = null;
    private TextView textView_capture_result_private = null;
    private TextView textView_capture_result_slave = null;

    Intent intent;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.capture);

        initCamera();
        DataUtil.instance().clearAll();

        btn_capture_master_code = (Button) findViewById(R.id.btn_capture_master_code);
        btn_capture_private_code = (Button) findViewById(R.id.btn_capture_private_code);
        btn_capture_slave_code = (Button) findViewById(R.id.btn_capture_slave_code);
        btn_capture_return = (Button) findViewById(R.id.btn_capture_returnto_mainui);

        textView_capture_result_master = (TextView) findViewById(R.id.textview_capture_result_master);
        textView_capture_result_private = (TextView) findViewById(R.id.textview_capture_result_private);
        textView_capture_result_slave = (TextView) findViewById(R.id.textview_capture_result_slave);
    }

    private void initCamera() {
        //To change body of created methods use File | Settings | File Templates.
        Toast.makeText(Capture2DCode.this,"正在初始化摄像头",Toast.LENGTH_SHORT).show();

        intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE","QR_CODE_MODE");
    }

    @Override
    protected void onStart() {
        super.onStart();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected void onResume() {
        super.onResume();    //To change body of overridden methods use File | Settings | File Templates.

        generateResultString();

        btn_capture_master_code.setOnClickListener(listener_btn_capture_master_code);
        btn_capture_private_code.setOnClickListener(listener_btn_capture_private_code);
        btn_capture_slave_code.setOnClickListener(listener_btn_capture_slave_code);
        btn_capture_return.setOnClickListener(new Listener_btn_return());

    }

    @SuppressLint("ResourceAsColor")
	private void generateResultString(){
        if(DataUtil.instance().getMasterCode() != null){
            textView_capture_result_master.setText("\t\t母章信息：扫描成功");
        }else{
            textView_capture_result_master.setText("\t\t母章信息：未扫描");
        }

        if(DataUtil.instance().getPrivateCode() != null){
            textView_capture_result_private.setText("\t\t私章信息：扫描成功");
        }else{
            textView_capture_result_private.setText("\t\t私章信息：未扫描");
        }

        if(DataUtil.instance().getSlaveCode() != null){
            textView_capture_result_slave.setText("\t\t子章信息：扫描成功");
        }else{
            textView_capture_result_slave.setText("\t\t子章信息：未扫描");
        }
    }

    private Button.OnClickListener listener_btn_capture_master_code = new Button.OnClickListener(){
        public void onClick(View view) {
            //To change body of implemented methods use File | Settings | File Templates.
            startActivityForResult(intent,0);
        }
    };

    private Button.OnClickListener listener_btn_capture_private_code = new Button.OnClickListener(){
        public void onClick(View view) {
            //To change body of implemented methods use File | Settings | File Templates.
            startActivityForResult(intent,1);
        }
    };

    private Button.OnClickListener listener_btn_capture_slave_code = new Button.OnClickListener(){
        public void onClick(View view) {
            //To change body of implemented methods use File | Settings | File Templates.
            startActivityForResult(intent,2);
        }
    };

    class Listener_btn_return implements View.OnClickListener{
        public void onClick(View view) {
            //To change body of implemented methods use File | Settings | File Templates.
            finish();
        }
    }

    public void onActivityResult(int requestCode,int resultCode,Intent intent){
        String code = intent.getStringExtra("SCAN_RESULT");
        if(resultCode == RESULT_OK){
            if(requestCode == 0){
                DataUtil.instance().setMasterCode(code);
                Log.v("master", code);
                Toast.makeText(this,"母章信息扫描成功",Toast.LENGTH_SHORT).show();
                DataUtil.instance().PSmasterCode();
            }else if(requestCode == 1){
            	DataUtil.instance().setPrivateCode(code);
            	Log.v("private", code);
                try {
					if(DataUtil.instance().PSprivateCode()){
					   Toast.makeText(this,"私章信息扫描成功",Toast.LENGTH_SHORT).show();
					}else{
					    AlertDialog.Builder builder = new AlertDialog.Builder(Capture2DCode.this);
					    builder.setTitle("警告").setMessage("当前扫描的私章属于伪造印章！").setIcon(R.drawable.wrong_1);
					    AlertDialog alertDialog = builder.create();
					    alertDialog.show();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }else if(requestCode == 2){
            	DataUtil.instance().setSlaveCode(code);
            	Log.v("slave", code);
                try{
					if(DataUtil.instance().PSslaveCode()){
					   Toast.makeText(this,"子章信息扫描成功",Toast.LENGTH_SHORT).show();
					}else{
					    AlertDialog.Builder builder = new AlertDialog.Builder(Capture2DCode.this);
					    builder.setTitle("警告").setMessage("当前扫描的子章属于伪造印章！").setIcon(R.drawable.wrong_1);
					    AlertDialog alertDialog = builder.create();
					    alertDialog.show();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        }else if(resultCode == RESULT_CANCELED){
            Toast.makeText(this, "扫描出现错误，请检查摄像头！", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(Capture2DCode.this,"扫描出现未知错误，请尝试重新扫描！",Toast.LENGTH_SHORT);
        }
    }

}